package com.gqz.aircraftwar.mvp.view

import android.graphics.Path
import android.graphics.PathMeasure
import android.graphics.PointF
import com.gqz.aircraftwar.App
import com.gqz.aircraftwar.mvp.model.aircraft.Aircraft
import java.lang.Exception
import kotlin.math.abs


/**
 * Descraption
 *
 * @author gqz
 * @data 2020/4/2
 */

class EnemyPath {

    object Holder {
        var instance = EnemyPath()
    }

    companion object {
        var xPath = XPath()
        var sPath = SPath()
        var uPath = UPath()
        var bPath = BossPath()
        var instance = Holder.instance

        fun reset() {
            xPath.reset()
            sPath.reset()
            uPath.reset()
            bPath.reset()
        }
    }
}

open class BasePath {
    var path = Path()
    var pm = PathMeasure()
    var distancePer = 0f
    var increment = 0.0007f
    var moveEnd = false

    open fun move(aircraft: Aircraft, offset: Float) {
        distancePer += increment
        moveEnd = distancePer > 1.0f
        var pos = FloatArray(2)
        var distance = pm.length * distancePer
        pm.getPosTan(distance - offset, pos, FloatArray(2))
        aircraft.pos.x = pos[0]
        aircraft.pos.y = pos[1]
    }

    fun reset() {
        distancePer = 0f
        moveEnd = false
    }
}

class XPath : BasePath() {
    init {
        increment = 0.00075f

        var startPos = PointF(App.surfaceW * 9f / 10f, -30f)
        var endPos = PointF(-200f, App.surfaceH * 6f / 10f)
        var ctrlPos = PointF(abs(startPos.x + endPos.x), abs(startPos.y + endPos.y))
        path.moveTo(startPos.x, startPos.y)
        path.quadTo(ctrlPos.x, ctrlPos.y, endPos.x, endPos.y)

        path.rLineTo(0f, -App.surfaceH * 7f / 10f)
        path.rLineTo(App.surfaceW / 5f, 0f)
        path.quadTo(
            App.surfaceW - ctrlPos.x,
            ctrlPos.y,
            App.surfaceW + 200f,
            App.surfaceH * 6f / 10f
        )
        pm.setPath(path, false)
    }
}

class SPath : BasePath() {
    init {
        increment = 0.0005f
        var ctrlY = 200f
        var ctrlY2 = ctrlY * 2
        var ctrlX = ctrlY * 3 / 4
        path.moveTo(-200f, 100f)
        path.lineTo(ctrlY, 100f)
        for (i in 1..2) {
            path.rLineTo(App.surfaceW - ctrlY2, 0f)
            path.rCubicTo(ctrlX, 0f, ctrlX, ctrlY, 0f, ctrlY)
            path.rLineTo(-App.surfaceW + ctrlY2, 0f)
            path.rCubicTo(-ctrlX, 0f, -ctrlX, ctrlY, 0f, ctrlY)
        }
        path.rLineTo(App.surfaceW.toFloat() * 2, 0f)
        pm.setPath(path, false)
    }
}

class UPath : BasePath() {
    init {
        increment = 0.0004f
        var margin = 100f
        var wide = App.surfaceW - margin * 2f
        path.moveTo(margin, -margin)
        path.lineTo(margin, 0f)
        path.rLineTo(0f, App.surfaceH * 2f / 4f)
        path.rCubicTo(
            0f, wide * 2 / 3, wide, wide * 2 / 3,
            wide, 0f
        )
        path.rLineTo(0f, -App.surfaceH.toFloat())
        pm.setPath(path, false)
    }
}

class BossPath : BasePath() {
    init {
        increment = 0.00025f
        path.moveTo(App.surfaceW / 9f, App.surfaceH / 10f)
        path.rLineTo(App.surfaceW * 7f / 9f, 0f)
        path.rLineTo(0f, App.surfaceH / 10f)
        path.rLineTo(-App.surfaceW * 3f / 9f, 0f)
        path.rLineTo(0f, App.surfaceH / 2f)
        path.rLineTo(0f, -App.surfaceH / 2f)
        path.rLineTo(-App.surfaceW * 4f / 9f, 0f)
        path.close()
        pm.setPath(path, false)
    }
}
